using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class EtyMain
{
	static void Main(string[] aPrm)
	{
		Console.WriteLine("A test for converting proto to json.");
		PkgUser.PhtList_Respond oRpd = new PkgUser.PhtList_Respond();
		oRpd.Code = 0x3001;
		oRpd.Command = "Usr_Pht_List";
		oRpd.Result = "0";
		oRpd.Message = "成功";
		PkgUser.PhtList_Item oItm = new PkgUser.PhtList_Item();
		oItm.PhtIdntifr = "1";
		oItm.PhtStatus = "1";
		oItm.UrlFinalztion = "https://www.baidu.com/1.jpg";
		oRpd.List.Add(oItm);

		Google.Protobuf.Reflection.TypeRegistry r = Google.Protobuf.Reflection.TypeRegistry.FromMessages(PkgUser.PhtList_Respond.Descriptor);
		Google.Protobuf.JsonFormatter j = new Google.Protobuf.JsonFormatter(new Google.Protobuf.JsonFormatter.Settings(false, r).WithPreserveProtoFieldNames(true));
		String sTxt = j.Format(oRpd);
		Console.WriteLine("PhtList_Respond is: " + sTxt);

		Byte[] aBfr = new Byte[1024];
		int nBfr = 0;
		Google.Protobuf.CodedOutputStream oOut = new Google.Protobuf.CodedOutputStream(aBfr);
		oRpd.WriteTo(oOut);
		nBfr = oRpd.CalculateSize();
		oRpd = null;

		PkgUser.MsgCommon_Respond oPkg = null;
		try {
			oPkg = PkgUser.MsgCommon_Respond.Parser.ParseFrom(aBfr, 0, 5);
		} catch (Exception e) {
			Console.WriteLine("Respond is error: " + e.Message);
		}
		if (oPkg != null) {
			switch (oPkg.Code) {
				case 0x3001: {
					try {
						oRpd = PkgUser.PhtList_Respond.Parser.ParseFrom(aBfr, 0, nBfr);
					} catch (Exception e) {
						Console.WriteLine("Respond is error: " + e.Message);
					}
				}
				break;
			}
		}

		aBfr = null;
		oRpd = null;

		sTxt = "{\"code\":12289,\"command\":\"Usr_Pht_List\",\"result\":\"0\",\"message\":\"成功\",\"list\":[{\"pht_idntifr\":\"1\",\"pht_status\":\"1\",\"url_finalztion\":\"https://www.baidu.com/1.jpg\"}]}";
		try {
			oRpd = PkgUser.PhtList_Respond.Parser.ParseJson(sTxt);
		} catch (Exception e) {
			Console.WriteLine("Respond is error: " + e.Message);
		}

		oRpd = null;
	}
}
